package com.umeox.watch.moto.apiclient.http.callback;


import com.alibaba.fastjson.JSONObject;
import com.umeox.watch.moto.apiclient.BuildConfig;
import com.umeox.watch.moto.apiclient.http.ApiEnum;
import com.umeox.watch.moto.apiclient.http.request.WaasRequest;
import com.umeox.watch.moto.apiclient.http.response.WaasResponse;
import com.umeox.watch.moto.apiclient.http.response.WaasResponseImpl;
import com.umeox.watch.moto.apiclient.model.AlarmDTO;
import com.umeox.watch.moto.apiclient.model.BuddyDTO;
import com.umeox.watch.moto.apiclient.model.ConcernTimeDTO;
import com.umeox.watch.moto.apiclient.model.ContactDTO;
import com.umeox.watch.moto.apiclient.model.DeviceConfigDTO;
import com.umeox.watch.moto.apiclient.model.ForbidPeriodDTO;
import com.umeox.watch.moto.apiclient.model.MessageDTO;
import com.umeox.watch.moto.apiclient.model.ServerConfigDTO;
import com.umeox.watch.moto.apiclient.model.StepDTO;
import com.umeox.watch.moto.apiclient.model.TokenDTO;
import com.umeox.watch.moto.apiclient.model.WallpaperDTO;
import com.umeox.watch.moto.apiclient.model.WifiDTO;

import java.lang.reflect.Type;

public class WaasJsonCallback implements WaasCallback {
    private WaasCallback callback;

    public WaasJsonCallback(WaasCallback callback) {
        this.callback = callback;
    }

    private WaasResponseImpl convert(WaasRequest request, WaasResponse response) {
        WaasResponseImpl waasResponse = new WaasResponseImpl();
        if (!response.isSuccessful()) {
            waasResponse.setMessage(BuildConfig.DEBUG ? response.getMessage() : "服务器繁忙，请稍后试试！");
        } else {
            try {
                JSONObject result = (JSONObject) response.getData();
                Type t = request.getApi().getTypeToken().getType();
                if (t != null) {
                    if (t.getClass().isAssignableFrom(String.class)) {
                        waasResponse.setData(result.getString("result"));
                    } else {
                        if(request.getApi() == ApiEnum.REFRESH_CONCERNTIME){
                            waasResponse.setData(JSONObject.toJavaObject(result.getJSONObject("result"), ConcernTimeDTO.class));
                        }else if(request.getApi() == ApiEnum.REFRESH_ALARMLIST){
                            waasResponse.setData(JSONObject.parseArray(result.getJSONArray("result").toJSONString(), AlarmDTO.class));
                        }else if(request.getApi() == ApiEnum.GET_BUDDIES){
                            waasResponse.setData(JSONObject.toJavaObject(result.getJSONObject("result"), BuddyDTO.class));
                        }else if(request.getApi() == ApiEnum.INIT_DEVICE_CONFIG){
                            waasResponse.setData(JSONObject.toJavaObject(result.getJSONObject("result"), DeviceConfigDTO.class));
                        }else if(request.getApi() == ApiEnum.GET_CONTACTS){
                            waasResponse.setData(JSONObject.toJavaObject(result.getJSONObject("result"), ContactDTO.class));
                        }else if(request.getApi() == ApiEnum.REFRESH_CLASSTIME){
                            waasResponse.setData(JSONObject.toJavaObject(result.getJSONObject("result"), ForbidPeriodDTO.class));
                        }else if(request.getApi() == ApiEnum.REFRESH_MESSAGE_LIST){
                            waasResponse.setData(JSONObject.toJavaObject(result.getJSONObject("result"), MessageDTO.class));
                        }else if(request.getApi() == ApiEnum.INIT_SERVER_CONFIG){
                            waasResponse.setData(JSONObject.toJavaObject(result.getJSONObject("result"), ServerConfigDTO.class));
                        }else if(request.getApi() == ApiEnum.GET_PETINFO){
                            waasResponse.setData(JSONObject.toJavaObject(result.getJSONObject("result"), StepDTO.class));
                        }else if(request.getApi() == ApiEnum.ACCESS_TOKEN){
                            waasResponse.setData(JSONObject.toJavaObject(result.getJSONObject("result"), TokenDTO.class));
                        }else if(request.getApi() == ApiEnum.GET_WALLPAPERS){
                            waasResponse.setData(JSONObject.toJavaObject(result.getJSONObject("result"), WallpaperDTO.class));
                        }else if(request.getApi() == ApiEnum.GET_WIFI_LIST){

                            waasResponse.setData(JSONObject.parseArray(result.getJSONArray("result").toString(), WifiDTO.class));
                        }else {
                            waasResponse.setData(JSONObject.toJavaObject(result.getJSONObject("result"), t.getClass()));
                        }
                    }
                }
                waasResponse.setCode(result.getInteger("status"));
                waasResponse.setMessage(result.getString("msg"));
            } catch (Exception e) {
                if (BuildConfig.DEBUG) {
                    e.printStackTrace();
                }
                waasResponse.setMessage(BuildConfig.DEBUG ? "返回结果格式错误" : "服务器繁忙，请稍后试试！");
            }
        }
        return waasResponse;
    }

    @Override
    public void onFailure(WaasRequest request, Exception e) {
        if (this.callback != null) {
            this.callback.onFailure(request, e);
        }
    }

    @Override
    public void onResponse(WaasRequest request, WaasResponse response) {
        if (this.callback != null) {
            this.callback.onResponse(request, convert(request, response));
        }
    }
}
